//
//  XLSwitchUserPresenter.m
//  industry
//
//  Created by xl on 2020/12/17.
//  Copyright © 2020 Haier. All rights reserved.
//

#import "XLSwitchUserPresenter.h"
#import "XLSwitchUserPresenterProtocol.h"
#import "XLSwitchUserModel.h"
#import "XLSwitchUserViewProtocol.h"

@interface XLSwitchUserPresenter ()<XLSwitchUserPresenterProtocol>

@end
@implementation XLSwitchUserPresenter

- (void)dealloc {
    XLLog(@"XLSwitchCompanyPresenter dealloc");
}

#pragma mark - XLSwitchCompanyPresenterProtocol
- (void)requestUserList {
    XLHttpRequest *request = [[XLHttpRequest alloc] init];
    WEAKSELF
    [request requestWithMethod:XLHttpMedthTypeGET url:XLSwitchUserListUrl params:nil isExtraParams:NO successBlock:^(NSDictionary *dict) {
        STRONGSELF
        id data = [dict objectForKey:@"data"];
        if ([data isKindOfClass:[NSArray class]]) {
            NSArray *temArr = [XLSwitchUserModel mj_objectArrayWithKeyValuesArray:data];
            XLSend(strongSelf.baseController.cView, XLSwitchUserViewProtocol, refreshTableView:temArr);
        }
    } failBlock:^(XLServerRequestsStatus status, XLNetworkReachabilityStatus reachability, NSError *error, NSDictionary *dict) {
        
    }];
}

- (void)requestAddNewClient:(NSString *)code {
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    if (!ISEmptyString(code)) {
        [params setValue:code forKey:@"inviteCode"];
    }
    XLHttpRequest *request = [[XLHttpRequest alloc] init];
    WEAKSELF
    XLSend(self.baseController.cView, XLSwitchUserViewProtocol, showLoadingAnimalWithTitle:@"加载中...");
    [request requestWithMethod:XLHttpMedthTypeGET url:XLAddNewClientUrl params:params isExtraParams:NO successBlock:^(NSDictionary *dict) {
        STRONGSELF
        XLSend(strongSelf.baseController.cView, XLSwitchUserViewProtocol, hideLoadingAnimal);
        [XLToast showSuccessMessage:@"绑定客户成功"];
        XLSend(strongSelf.baseController.cView, XLSwitchUserViewProtocol, hideAddClientAlertView);
    } failBlock:^(XLServerRequestsStatus status, XLNetworkReachabilityStatus reachability, NSError *error, NSDictionary *dict) {
        STRONGSELF
        XLSend(strongSelf.baseController.cView, XLSwitchUserViewProtocol, hideLoadingAnimal);
    }];
}

- (void)handleCellData:(id)cellData {
    if ([cellData isKindOfClass:[XLSwitchUserModel class]]) {
        XLSwitchUserModel *model = (XLSwitchUserModel *)cellData;
        XL_UserInfoData.employeeName = model.cusName;
        [[XLUserData sharedXLUserData] saveCurrentUserInfo:XL_UserInfoData];
        [self.baseController.navigationController popViewControllerAnimated:YES];
    }
}

@end
